<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\Admin\ModelFieldModel;
use App\Model\Admin\SystemMenuModel;
use App\Model\Admin\SystemModel;
use Illuminate\Http\Request;

class ContentController extends Controller
{


    public function index($type)
    {
        $data['type'] = $type;
        if (!\Schema::hasTable($type)){
            return '访问的表不存在';
        }
        // 生成自定义表格title
        $table_title = ModelFieldModel::where('is_table_title',1)
            ->where('model.table_name',$type)
            ->join('model','model.id','=','model_field.model_id')
            ->select(['model_field.form_name','model_field.name'])
            ->get();
        $cols = [];
        foreach ($table_title as $v){
            $cols[] = [
                'field' => $v->name,
                'title' => $v->form_name,
                'align' => 'center'
            ];
        };
        array_unshift($cols,['type' => 'numbers']);
        array_push($cols,
            ['field' => 'created_at','title'=>'添加时间'],
            ['templet'=>'#auth-state','width'=>120,'align'=>'center','title'=>'操作']
        );
        $data['cols'] = json_encode($cols);

        return view('admin.content.index')->with('data',$data);
    }

    /**
     * 返回列表数据
     *
     * @param Request $request
     * @param $type
     * @return mixed
     */
    public function list(Request $request, $type)
    {
        $data['type'] = $type;

        $page = $request->page ? : 1;
        $limit = $request ->limit ? : 10;
        $data['code'] = 0;
        $data['msg'] = '成功';
        $data['count'] = \DB::table($type)->count();
        $data['data'] = \DB::table($type)
            ->skip($limit * ($page -1))
            ->take($limit)
            ->get();

        return $data;
    }

    /**
     * 添加
     *
     * @param $type
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create($type)
    {
        $data['model_fields'] = ModelFieldModel::where('model.table_name',$type)
            ->join('model','model.id','=','model_field.model_id')
            ->select(['model_field.*'])
            ->orderBy('order')
            ->get();
        $data['type'] = $type;

        return view('admin.content.add')->with('data',$data);
    }


    /**
     * 保存
     *
     * @param Request $request
     * @param $type
     * @return array
     */
    public function store(Request $request,$type)
    {
        if (!$this->hasTable($type)){
            return $this->res_msg(1,'没有数据表');
        }
        $fields = ModelFieldModel::where('model.table_name',$type)
            ->join('model','model.id','=','model_field.model_id')
            ->select(['model_field.name'])
            ->orderBy('order')
            ->get()->pluck('name')->toArray();
        $data = $request->only($fields);
        $data['created_at'] = date('YmdHms',time());
        $data['updated_at'] = date('YmdHms',time());
        try {
            \DB::table($type)->insert($data);
            return $this->res_msg(0,'请求成功');
        } catch (\Exception $exception){
            return $this->res_msg(1,'请求失败');
        }
    }


    /**
     * 添加
     *
     * @param $type 内容表
     * @param $id 修改ID
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($type,$id)
    {
        if (!$this->hasTable($type)){
            return $this->res_msg(1,'没有数据表');
        }
        $data['model_fields'] = ModelFieldModel::where('model.table_name',$type)
            ->join('model','model.id','=','model_field.model_id')
            ->select(['model_field.*'])
            ->orderBy('order')
            ->get();
        $content = \DB::table($type)->find($id);
        $data['type'] = $type;
        return view('admin.content.add')->with('data',$data)->with('content',$content);
    }


    /**
     * 执行修改
     *
     * @param Request $request
     * @param $type
     * @param $id
     * @return array
     */
    public function update(Request $request,$type, $id)
    {
        $fields = ModelFieldModel::where('model.table_name',$type)
            ->join('model','model.id','=','model_field.model_id')
            ->select(['model_field.name'])
            ->orderBy('order')
            ->get()->pluck('name')->toArray();
        $data = $request->only($fields);
        $data['updated_at'] = date('YmdHms',time());

        try {
            \DB::table($type)->where('id',$id)->update($data);
            return $this->res_msg(0,'请求成功');
        } catch (\Exception $exception){
            return $this->res_msg(1,'请求失败');
        }
    }


    /**
     * 删除
     *
     * @param $type
     * @param $id
     * @return array
     */
    public function destroy($type,$id)
    {
        try {
            \DB::table($type)->where('id',$id)->delete();
            return $this->res_msg(0,'删除成功');
        } catch (\Exception $exception){
            return $this->res_msg(1,'删除失败');
        }
    }

    /**
     * 判断是否有数据表
     *
     * @param $table_name
     * @return bool
     */
    public function hasTable($table_name)
    {
        if (!\Schema::hasTable($table_name)){
            return false;
        }
        return true;
    }
}
